﻿using DAL.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.FKTZ
{
    public class FktzAttachmentDAL
    {
        private readonly FktzDataContext _dataContext;

        public FktzAttachmentDAL(FktzDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public async Task CreateAsync(IEnumerable<FktzAttachment> entities)
        {
            foreach (var item in entities)
            {
                await CreateAsync(item);
            }
        }
        public async Task SaveChangeAsync()
        {
            _dataContext.SaveChangesAsync();
        }
        public async Task CreateAsync(FktzAttachment entity)
        {
            _dataContext.Entry(entity).State = EntityState.Added;
        }

        public async Task SaveChagnesAsync()
        {
            await _dataContext.SaveChangesAsync();
        }

        public  Task<IEnumerable<FktzAttachment>> ListAsync(string businessId, string option = null)
        {
            var query = _dataContext.Attachments.AsNoTracking().AsQueryable();
            if (!string.IsNullOrEmpty(businessId)) query = query.Where(x => x.BusinessId == businessId);
            return Task.FromResult(query as IEnumerable<FktzAttachment>);

            //var id = businessId.ToString();
            //var query = from q in _dataContext.Attachments.AsNoTracking()
            //            where q.BusinessId == id
            //            select q;
            //return  query.ToListAsync();
        }

        public async Task<FktzAttachment> GetAsync(string id)
        {
            return await _dataContext.Attachments.FindAsync(id);
        }

        public async Task DeleteAsync(IEnumerable<FktzAttachment> entities)
        {
            foreach (var item in entities)
            {
                await DeleteAsync(item);
            }
        }

        public async Task DeleteAsync(FktzAttachment entity)
        {
            _dataContext.Entry(entity).State = EntityState.Deleted;
        }
    }
}
